﻿using System.ComponentModel;
using MyFloridaHouse.libSvUtilities.Commands;
using System;
using System.Windows.Controls;

namespace MyFloridaHouse.libSvOpenDistricts.Dialogs
{
    public class _BaseDlg : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Commands
        public DelegateCommand CmdOK { get; set; }
        public DelegateCommand CmdCancel { get; set; }

        public void IntializeCommands()
        {
            CmdOK = DelegateCommand.CreateCommand(this, CmdOKExecute, CmdOKCanExecute);
            CmdCancel = DelegateCommand.CreateCommand(this, CmdCancelExecute, CmdCancelCanExecute);
        }

        public void CmdOKExecute(object parameter)
        {
            Done(true);
        }
        public void CmdCancelExecute(object parameter)
        {
            Done(false);
        }
        public  bool CmdCancelCanExecute(object parameter)
        {
            return true;
        }

        public virtual  bool CmdOKCanExecute(object parameter)
        {
            return true;
        }



        #endregion

        #region Window Control

        internal void InitializeDlgWindow(PropertyChangedEventHandler propertyChangedEventHandler, StackPanel childWindowStackPanel, UserControl dlgWindow)
        {
            PropertyChanged += propertyChangedEventHandler;

            IntializeCommands();
            // add the window to the application window 
            childWindowStackPanel.Children.Add(dlgWindow);
            dlgWindow.DataContext = this;
        }

        private bool _isDlgActive = false;
        public bool isDlgActive
        {
            get { return _isDlgActive; }
            set
            {
                if (_isDlgActive != value)
                {
                    _isDlgActive = value;
                    NotifyPropertyChanged("isDlgActive");
                }
            }
        }

        private Action<bool> OnDlgComplete;

        public virtual void Start(Action<bool> OnDlgComplete)
        {
            isDlgActive = true;
            this.OnDlgComplete = OnDlgComplete;
        }

        internal void Done(bool result)
        {
            isDlgActive = false;
            OnDlgComplete(result);
        }

        #endregion

    }
}
